package com.leo.blog.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.LinkDao;
import com.leo.blog.dto.LinkExecution;
import com.leo.blog.entity.Link;
import com.leo.blog.enums.LinkStateEnum;
import com.leo.blog.exceptions.LinkOperationException;

import com.leo.blog.service.LinkService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.PageCalculator;

@Service
public class LinkServiceImpl implements LinkService{
	@Autowired
	private LinkDao linkDao;


	
	private static Logger logger = LoggerFactory.getLogger(LinkServiceImpl.class);
	
	@Override
	public LinkExecution getAllLinkList() {
		List<Link> linkList = new ArrayList<>();
		linkList = linkDao.queryAllLinkList();
		if(linkList.size() > 0) {
			return new LinkExecution(LinkStateEnum.SUCCESS, linkList);
		}else {
			return new LinkExecution(LinkStateEnum.OFFLINE);
		}
	}

	@Override
	public LinkExecution getLinkList(Link linkCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,待用dao层返回相关类型列表
		List<Link> linkList = linkDao.queryLinkList(linkCondition, rowIndex, pageSize);	
		//根据相同的查询条件,返回类型总数
		int count =linkDao.queryLinkListCount(linkCondition);
		LinkExecution le = new LinkExecution();
		if(linkList != null) {
			le.setLinkList(linkList);
			le.setCount(count);
		}else {
			le.setState(LinkStateEnum.INNER_ERROR.getState());
		}
		return le;
	}

	@Override
	public LinkExecution getLinkById(int linkId) {
		Link link = linkDao.queryLinkById(linkId);
		LinkExecution le = new LinkExecution();
		if(link != null) {		
			le.setLink(link);
			le.setState(LinkStateEnum.SUCCESS.getState());
		}else {
			le.setState(LinkStateEnum.INNER_ERROR.getState());
		}
		return le;
	}

	@Override
	@SystemLog(description = ConstantUtil.LINKS_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN)
	public int editLink(String name, Link link) {
		if(link == null) {
			return 0;
		}else {
			int effectedNum = linkDao.updateLink(link);
			if(effectedNum > 0) {
				return effectedNum;
			}else {
				return 0;
			}
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.LINKS_DELETE,userType=ConstantUtil.USERTYPE_ADMIN)
	public int deleteLink(String name, int linkId) {
		if(linkId > 0) {
			int effectedNum = linkDao.deleteLink(linkId);
			if(effectedNum > 0) {
				return effectedNum;
			}else {
				return 0;
			}
		}else {
			return 0;
		}		
	}

	@Override
	@SystemLog(description = ConstantUtil.LINKS_ADD,userType=ConstantUtil.USERTYPE_USER)
	public int addLink(String name, Link link) {
		if(link != null) {
			//初始化
			link.setClickNum(0);
			link.setCreateTime(new Date());
			
			int effectedNum = linkDao.insertLink(link);
			if(effectedNum > 0) {
				return effectedNum;
			}else {
				return 0;
			}
		}else {
			return 0;
		}
	}

}
